/* =============================================================
 * Ketchup4j : Squeeze the speed out of your objects. 
 * =============================================================
 *
 * (C) Copyright 2007, by Saverio Miroddi
 *
 * Project Info:  http://tinyhorrorsql.sourceforge.net/
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or (at 
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------------------
 * LFUCacheObject.java
 * ---------------------------
 * Author: Saverio Miroddi
 * 
 */
package org.itmozart.ketchup4j.other;

import java.lang.ref.SoftReference;


/**
 * We do not use neither hashCode (no hashing involved) nor equals (we use
 * references), because TreeMap uses only compareTo, because it is a binary tree.
 */
@SuppressWarnings("unchecked")
public final class LFUCacheObject<K, V> implements Comparable {

	private static final int REF_STRONG = 0;
	private static final int REF_SOFT 	= 1;
	
	private static int idCounter = 0;

	private int id;
	private int referenceType;

	public K objectId;
	public Object object;

	public long lastTimeAccessed;
	public int memorySize;

	public int accessCount;
	
	public LFUCacheObject(K objectId, V object, int memorySize) {
		id = idCounter++; // !! ++ !!
		this.objectId = objectId;
		this.memorySize = memorySize;

		this.lastTimeAccessed = System.currentTimeMillis();
		
		this.referenceType = REF_STRONG;
		this.object = object;
		
		/* 4 billions access should be enough ;-) 
		 * we can use negative values as accessCount isn't absolute, but
		 * relative. */
		accessCount = Integer.MIN_VALUE;
	}

	/**
	 * Only updates access count and last access.
	 */
	public void update() {
		accessCount++;
		lastTimeAccessed = System.currentTimeMillis();
	}
	
	/**
	 * <b>WARNING!!</b>This must be called AFTER the object has been removed from the tree, 
	 * because updates access count, so corrupts the search inside the tree.
	 */
	public void updateStrong(V newObject, int newMemSize) {
		accessCount++;
		lastTimeAccessed = System.currentTimeMillis();

		this.object = newObject;
		this.memorySize = newMemSize;
	}
	
	public final void updateToSoft() {
		// don't care about access time
		
		object = new SoftReference<V>((V)object);
		referenceType = REF_SOFT;
	}
	
	/**
	 * This method must be called, if needed, after getObject.<br>
	 * Warning! There must be a strong reference to the internal object before
	 * calling this method.
	 */
	public final void updateToStrong() {
		accessCount++;
		lastTimeAccessed = System.currentTimeMillis();

		object = ((SoftReference<V>)object).get();
		referenceType = REF_STRONG;
	}
	
	/**
	 * We need to use this because an object could change while cleaner
	 * is cleaning, and if this happen cleaner can't know which type
	 * object is (in case of ICache.REF_MIXED).
	 */
	public final V getObject() {
		switch(referenceType) {
		case REF_STRONG: return (V)object;
		default: return ((SoftReference<V>)object).get();
		}
	}
	
	public int compareTo(Object o2) {
		LFUCacheObject co2 = (LFUCacheObject) o2;
		
		int idDiff = id - co2.id; // WARNING! use the opp
		
		if (idDiff == 0) return 0; // same id --> same reference, more solid

		int accessDifference = accessCount - co2.accessCount;

		if (accessDifference != 0) return accessDifference;
		/* We must use id because the time difference is not reliable,
		 * because it can have too short time slices */
		else return idDiff;
	}
	
	@Override
	public String toString() {
		return object.toString() + "[I" + id + "/A" + accessCount + "]";
	}
}